package com.ifcreate.infowell.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.ifcreate.infowell.common.util.LongToDateStrSerializer;
import com.ifcreate.infowell.common.util.StringToArraySerializer;
import lombok.Data;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author chenzedeng
 * @Email yustart@foxmail.com
 * @Create 2020-10-25 4:35 下午
 */
@NodeEntity
@Data
public class InfoWellNode {

    @Id
    @GeneratedValue
    private Long id;

    /**
     * 节点名称
     */
    @NotBlank(message = "name not blank")
    private String name;

    /**
     * 是否是新闻节点 true是 false其他节点
     */
    @NotNull(message = "isNews not null")
    private Boolean isNews;


    /**
     * 节点类型 (可为新闻节点的类型或者其他节点的类型标记)
     */
    private Integer type;

    /**
     * 内容标题
     */
    @NotBlank(message = "title not blank")
    private String title;

    /**
     * 内容提供者
     */
    @NotBlank(message = "infoProvider not blank")
    private String infoProvider;

    /**
     * 信息内容
     */
    @NotBlank(message = "infoContent not blank")
    private String infoContent;

    /**
     * 图片 JSON array string
     */
    @JsonSerialize(using = StringToArraySerializer.class)
    private String images;

    /**
     * 点击次数
     */
    private Long clickCount;

    /**
     * 生成时间
     */
    @JsonSerialize(using = LongToDateStrSerializer.class)
    private Long time;


    @JsonIgnore
    @Relationship(type = "InfoWellRelation")
    private Set<InfoWellRelation> relations;


    /**
     * 添加节点
     *
     * @param that
     */
    public void addRelation(InfoWellRelation that) {
        if (this.relations == null) {
            this.relations = new HashSet<>();
        }
        this.relations.add(that);
    }
}
